﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public class MoveSystem : ECSSystemtBase
{
    public override Type[] GetFilter()
    {
        return new Type[]
        {
            typeof(MoveComponent),
        };
    }

    public override void FixedUpdate(int delaTime)
    {
        base.FixedUpdate(delaTime);
    }

    private MoveComponent mc;
    private ColisionComponent cc;
    private void UpdateMove(ECSEntityBase entity, int deltaTime)
    {
        mc = (MoveComponent) entity.GetCom("MoveComponent");
        SyncVector3 newPos = mc.pos.DeepCopy();

        newPos.x += (mc.dir.x * deltaTime / 1000) * mc.m_velocity / 1000;
        newPos.y += (mc.dir.y * deltaTime / 1000) * mc.m_velocity / 1000;
        newPos.z += (mc.dir.z * deltaTime / 1000) * mc.m_velocity / 1000;

        if (entity.GetExistCom("FlyObjectComponent")&&
            entity.GetExistCom("CollisionComponent"))
        {
            cc = (ColisionComponent) entity.GetCom("CollisionComponent");
            cc.area.position = newPos.ToVector3();

            if (!IsCollisionBlock(cc.area))
            {
                mc.pos = newPos;
            }
            
        }
        else
        {
            mc.pos = newPos;
        }

        if (EcsSyncDebugSystem.isDebug&&EcsSyncDebugSystem.IsFilter("MoveSystem"))
        {
            string content = "id: " + mc.Entity.ID + " m_pos " + mc.pos.ToVector3() + " deltaTime " + deltaTime + " m_velocity " + mc.m_velocity + " m_dir " + mc.dir.ToVector();
            //Debug.Log(content);

            //SyncDebugSystem.syncLog += content + "\n";
        }
    }

    public bool IsCollisionBlock(Area area)
    {
        List<ECSEntityBase> list = GetEntityList(new string[] { "CollisionComponent", "BlockComponent" });
        for (int i = 0; i < list.Count; i++)
        {
            ColisionComponent cc = list[i].GetCom<ColisionComponent>();
            if (cc.area.AreaColideSucceed(area))
            {
                return true;
            }
        }

        return false;
    }
}

